// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <chrono>
#include <initializer_list>
#include <string>
#include <thread>
#include <utility>

#include <ktest/ktest.h>
#include <deneb/lru.h>

using namespace deneb;
using namespace std::chrono_literals;

TEST(TimedCacheTest, ContainsRespectsExpiration) {
  TimedCache<int, int> cache(2ms);

  cache.insert(1, 2);
  ASSERT_EQ(cache.size(), 1);
  ASSERT_TRUE(cache.contains(1));
  ASSERT_EQ(cache[1], 2);

  std::this_thread::sleep_for(1ms);

  EXPECT_TRUE(cache.contains(1));

  std::this_thread::sleep_for(1ms);

  EXPECT_FALSE(cache.contains(1));
}

TEST(TimedCacheTest, KnowsWhenAllKeysHaveExpired) {
  TimedCache<int, int> cache(2ms);

  ASSERT_TRUE(cache.all_expired());

  cache = {{1, 2}, {2, 3}};

  ASSERT_EQ(cache.size(), 2);
  ASSERT_FALSE(cache.all_expired());

  std::this_thread::sleep_for(1ms);

  cache.insert(3, 4);

  std::this_thread::sleep_for(1ms);

  EXPECT_FALSE(cache.all_expired());
  EXPECT_FALSE(cache.contains(1));
  EXPECT_FALSE(cache.contains(2));
  EXPECT_TRUE(cache.contains(3));

  std::this_thread::sleep_for(1ms);

  EXPECT_TRUE(cache.all_expired());
  EXPECT_FALSE(cache.contains(1));
  EXPECT_FALSE(cache.contains(2));
  EXPECT_FALSE(cache.contains(3));
}

TEST(TimedCacheTest, CleanExpiredRemovesExpiredElements) {
  TimedCache<int, int> cache(2ms, 128, {{1, 2}, {2, 3}});

  ASSERT_EQ(cache.size(), 2);
  ASSERT_FALSE(cache.all_expired());

  std::this_thread::sleep_for(1ms);

  cache.insert(3, 4);
  ASSERT_EQ(cache.size(), 3);

  std::this_thread::sleep_for(1ms);

  ASSERT_EQ(cache.size(), 3);

  cache.clear_expired();

  EXPECT_EQ(cache.size(), 1);
  EXPECT_FALSE(cache.contains(1));
  EXPECT_FALSE(cache.contains(2));
  EXPECT_TRUE(cache.contains(3));

  std::this_thread::sleep_for(1ms);

  cache.clear_expired();

  EXPECT_FALSE(cache.contains(3));
  EXPECT_EQ(cache.size(), 0);
  EXPECT_TRUE(cache.is_empty());
}

TEST(TimedCacheTest, LookupThrowsWhenKeyExpired) {
  TimedCache<int, int> cache(2ms, 128, {{1, 2}});

  ASSERT_EQ(cache.lookup(1), 2);

  std::this_thread::sleep_for(2ms);

  ASSERT_THROW(cache.lookup(1), deneb::deneb_error::KeyExpired);
}

TEST(TimedCacheTest, HasExpiredReturnsFalseForNonContainedKeys) {
  TimedCache<int, int> cache(2ms);

  EXPECT_FALSE(cache.has_expired(1));
  EXPECT_FALSE(cache.has_expired(2));
}

TEST(TimedCacheTest, HasExpiredReturnsFalseForContainedButNotExpiredKeys) {
  TimedCache<int, int> cache(100ms);

  cache.emplace(1, 1);
  cache.emplace(2, 2);

  EXPECT_FALSE(cache.has_expired(1));
  EXPECT_FALSE(cache.has_expired(2));
}

TEST(TimedCacheTest, HasExpiredReturnsTrueForContainedAndExpiredKeys) {
  TimedCache<int, int> cache(2ms);

  cache.emplace(1, 1);

  std::this_thread::sleep_for(1ms);

  cache.emplace(2, 2);

  EXPECT_FALSE(cache.has_expired(1));

  std::this_thread::sleep_for(1ms);

  EXPECT_TRUE(cache.has_expired(1));
  EXPECT_FALSE(cache.has_expired(2));

  std::this_thread::sleep_for(3ms);

  EXPECT_TRUE(cache.has_expired(1));
  EXPECT_TRUE(cache.has_expired(2));
}

TEST(TimedCacheTest, LookupsMoveElementsToFront) {
  TimedCache<std::string, int> cache(1s);

  cache.capacity(2);
  cache.insert({{"one", 1}, {"two", 2}});

  // The LRU principle mandates that lookups place
  // accessed elements to the front. So when we look at
  // one it should move to the front.

  auto iterator = cache.find("one");
  cache.emplace("three", 3);

  EXPECT_TRUE(cache.contains("one"));
  EXPECT_FALSE(cache.contains("two"));
  EXPECT_TRUE(cache.contains("three"));
  EXPECT_EQ(++iterator, cache.end());
}
